﻿// -----------------------------------------------------------------------
// <copyright file="Resize.cs" company="fcis">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace IPPackage.ImageOperation
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Resize : Open
    {
        /// <summary>
        /// Resizes the Image.
        /// </summary>
        /// <param name="type">The type of resize.</param>
        /// <param name="newWidth">The new width.</param>
        /// <param name="newHeight">The new height.</param>
        /// <returns>a new image</returns>
        public Bitmap ResizeI(string type, int newWidth, int newHeight)
        {
            return this.Resize_bilinear(newWidth, newHeight);    
        }

        /// <summary>
        /// use way bilinear to resize image.
        /// </summary>
        /// <param name="newWidth">The new width of image.</param>
        /// <param name="newHeight">The new height of image.</param>
        /// <returns>new image</returns>
        public Bitmap Resize_bilinear(int newWidth, int newHeight)
        {
            Bitmap newImg;
            try
            {
                int oldHeight = BufferOfImage.GetLength(1);
                int oldWidth = BufferOfImage.GetLength(0);
                newImg = new Bitmap(newWidth, newHeight);
                NewBufferOfImage = new Color[newWidth, newHeight];
                for (int i = 0; i < newHeight; i++)
                {
                    for (int j = 0; j < newWidth; j++)
                    {
                        double widRatio = (double)oldWidth / newWidth;
                        double heiRatio = (double)oldHeight / newHeight;
                        double oldX = j * widRatio;
                        double oldY = i * heiRatio;

                        double x1 = Math.Floor(oldX);
                        double x2 = x1 + 1;
                        if (x2 == oldWidth)
                        {
                            x2--;
                        }

                        double y1 = Math.Floor(oldY);
                        double y2 = y1 + 1;
                        if (y2 == oldHeight)
                        {
                            y2--;
                        }

                        Color p1 = this.BufferOfImage[(int)x1, (int)y1];
                        Color p2 = this.BufferOfImage[(int)x2, (int)y1];
                        Color p3 = this.BufferOfImage[(int)x1, (int)y2];
                        Color p4 = this.BufferOfImage[(int)x2, (int)y2];
                        ////3-Calculate X, Y fractions
                        double xfraction = oldX - x1;
                        double yfraction = oldY - y1;
                        ////4-Interpolate in X-Direction
                        Color z1 = new Color();
                        double z1R = (p1.R * (1 - xfraction)) + (p2.R * xfraction);
                        double z1G = (p1.G * (1 - xfraction)) + (p2.G * xfraction);
                        double z1B = (p1.B * (1 - xfraction)) + (p2.B * xfraction);
                        z1 = Color.FromArgb((int)z1R, (int)z1G, (int)z1B);
                        Color z2 = new Color();
                        double z2R = (p3.R * (1 - xfraction)) + (p4.R * xfraction);
                        double z2G = (p3.G * (1 - xfraction)) + (p4.G * xfraction);
                        double z2B = (p3.B * (1 - xfraction)) + (p4.B * xfraction);
                        z2 = Color.FromArgb((int)z2R, (int)z2G, (int)z2B);
                        ////5-Interpolate in Y-Direction
                        Color newPixel = new Color();
                        double newPixelR = (z1.R * (1 - yfraction)) + (z2.R * yfraction);
                        double newPixelG = (z1.G * (1 - yfraction)) + (z2.G * yfraction);
                        double newPixelB = (z1.B * (1 - yfraction)) + (z2.B * yfraction);
                        newPixel = Color.FromArgb((int)newPixelR, (int)newPixelG, (int)newPixelB);
                        NewBufferOfImage[j, i] = newPixel;
                        newImg.SetPixel(j, i, NewBufferOfImage[j, i]);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                newImg = null;
            }

            return newImg;
        }
    }
}